﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Tulde.MP3tunes.Animation
{
    public class OpacityAnimation : AnimationBase
    {
        public static TimeSpan DefaultDuration = TimeSpan.FromMilliseconds(750);

        public static TimeSpan Slow = TimeSpan.FromMilliseconds(3000);
        public static TimeSpan Normal = TimeSpan.FromMilliseconds(1500);
        public static TimeSpan Fast = TimeSpan.FromMilliseconds(500);

        public OpacityAnimation(double opacity)
            : this(opacity, DefaultDuration)
        {

        }

        public OpacityAnimation(double opacity, TimeSpan duration)
        {
            Opactity = opacity;
            Duration = duration;
        }

        public double Opactity
        {
            get;
            private set;
        }

        public TimeSpan Duration
        {
            get;
            private set;
        }

        protected override Storyboard CreateStoryboard()
        {
            var storyboard = new Storyboard();

            var opacityAnimation = new DoubleAnimation();

            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(FrameworkElement.Opacity)"));

            storyboard.Children.Add(opacityAnimation);

            return storyboard;
        }

        protected override void ApplyValues(Storyboard storyboard, TimeSpan? duration = null)
        {
            if (storyboard == null)
                throw new ArgumentNullException("storyboard");

            var opacityAnimation = storyboard.Children[0] as DoubleAnimation;

            opacityAnimation.To = Opactity;
            opacityAnimation.Duration = duration ?? Duration;
        }
    }

}
